package com.prd.controller;


import com.prd.dto.CustomerDTO;
import com.prd.service.CustomerService;
import com.prd.util.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 客户总表 前端控制器
 * </p>
 *
 * @author o1
 * @since 2023-11-03
 */
@Api(tags={"客户管理-客户"})
@RestController
@RequestMapping("/api/customer")
public class CustomerController {
    private CustomerService customerService;

    public CustomerController(CustomerService customerService) {
        this.customerService = customerService;
    }


    @ApiOperation("客户模块-客户-分页+筛选")
    @GetMapping("page")
    public JsonData pageOutsideList(
            @ApiParam(value = "当前页数") @RequestParam(value = "page", defaultValue = "1") int page,
            @ApiParam(value = "当页数目") @RequestParam(value = "size", defaultValue = "10") int size,
            @ApiParam(value = "客户类型: 全部-1，项目类1，供应链类2，服务类3，渠道类4，消费类5") @RequestParam(value = "customer_type", defaultValue = "-1") int customer_type,
            @ApiParam(value = "客户类别: 全部-1，A类1，B类2，C类3 ... ") @RequestParam(value = "customer_level", defaultValue = "-1") int customer_level,
            @ApiParam(value = "客户状态：全部-1，洽谈中客户1，被候选客户2，合作中客户3，潜在客户4，准落户5") @RequestParam(value = "customer_status", defaultValue = "-1") int customer_status,
            @ApiParam(value = "业务负责人id：全部-1，") @RequestParam(value = "business_manager_id", defaultValue = "-1") int business_manager_id,
            @ApiParam(value = "业务部门id全部-1，") @RequestParam(value = "business_department_id", defaultValue = "-1") int business_department_id,
            @ApiParam(value = "业务进度：全部-1，") @RequestParam(value = "business_status", defaultValue = "-1") int business_status,
            @ApiParam(value = "状态：正常0，公共资源库1，已销户2，已隐藏3") @RequestParam(value = "status", defaultValue = "-1") int status,
            @ApiParam(value = "地区") @RequestParam(value = "area", defaultValue = "") String area,
            @ApiParam(value = "创建年月") @RequestParam(value = "date", defaultValue = "") @DateTimeFormat(pattern = "yyyy-MM") Date date
    ) {
        Map<String, Object> pageResult = customerService.getAllCustomer(
                page, size, customer_type, customer_level, customer_status, business_manager_id, business_department_id, business_status, status, area, date
        );

        return JsonData.buildSuccess(pageResult);
    }

    @ApiOperation("客户模块-客户-单一导入/新建")
    @PostMapping(value = "add", produces = "application/json;charset=UTF-8")
    public JsonData insertCustomer(@ApiParam("客户-单一导入/新建") @RequestBody CustomerDTO customerDTO) {
        return customerService.addCustomer(customerDTO);
    }


    @ApiOperation("客户模块-客户-强行回收至公共资源库. 强行移交，隐藏，删除" +
            "0 = 正常, 1 = 公共资源, 2 = 已删除, 3 = 已隐藏")
    @GetMapping("changeStatus")
    public JsonData changeStatus(
            @ApiParam(value = "客户编号") @RequestParam(value = "customer_no", defaultValue = "1") String customerNo,
            @ApiParam(value = "状态码") @RequestParam(value = "status", defaultValue = "1") int status) {
        return customerService.changeStatus(customerNo, status);
    }

    @ApiOperation("客户模块-客户-模糊搜索")
    @GetMapping("search")
    public JsonData searchKeyWord(
            @ApiParam(value = "当前页数") @RequestParam(value = "page", defaultValue = "1") int page,
            @ApiParam(value = "当页数目") @RequestParam(value = "size", defaultValue = "10") int size,
            @ApiParam(value = "关键字") @RequestParam(value = "keyWord", defaultValue = "") String keyWord
    ) {

        Map<String, Object> pageResult = customerService.queryCustomer(page, size, keyWord);
        return JsonData.buildSuccess(pageResult);
    }

    @ApiOperation("客户模块-客户-更换业务负责人")
    @GetMapping("changeBusinessManager")
    public JsonData changeBusinessManager(
            @ApiParam(value = "客户编号") @RequestParam(value = "customer_no", defaultValue = "") String customerNo,
            @ApiParam(value = "业务负责人") @RequestParam(value = "bus_manager", defaultValue = "") String bus_manager,
            @ApiParam(value = "业务部门") @RequestParam(value = "bus_department", defaultValue = "") String bus_department,
            @ApiParam(value = "业务负责人电话") @RequestParam(value = "bus_phone", defaultValue = "") String bus_phone,
            @ApiParam(value = "业务负责人id") @RequestParam(value = "bus_manager_id", defaultValue = "") int bus_manager_id) {
        return customerService.updateBusinessManagerInfo(customerNo, bus_manager, bus_department, bus_phone, bus_manager_id);
    }


    @ApiOperation("客户模块-客户-UNI-新增详细报备")
    @PostMapping(value = "uAddDetail", produces = "application/json;charset=UTF-8")
    public JsonData uAddDetail(
            @ApiParam(value = "uAddDetail") @RequestBody CustomerDTO customerDTO) {
        return customerService.uAddDetail(customerDTO);
    }

    @ApiOperation("客户模块-客户-UNI-新增简易报备")
    @PostMapping(value = "uAddSimple", produces = "application/json;charset=UTF-8")
    public JsonData uAddSimple(
            @ApiParam(value = "uAddSimple") @RequestBody CustomerDTO customerDTO) {
        return customerService.uAddSimple(customerDTO);
    }

    @ApiOperation("客户模块-客户-UNI-合并简易报备")
    @PostMapping(value = "uMerge", produces = "application/json;charset=UTF-8")
    public JsonData uMerge(
            @ApiParam(value = "uMerge") @RequestBody CustomerDTO customerDTO) {
        return customerService.uMerge(customerDTO);
    }

    @ApiOperation("客户模块-客户-UNI-新增分公司/分店")
    @PostMapping(value = "uAddSubDetail", produces = "application/json;charset=UTF-8")
    public JsonData uAddSubDetail(
            @ApiParam(value = "uAddSubDetail") @RequestBody CustomerDTO customerDTO) {
        return customerService.uAddSubDetail(customerDTO);
    }

    @ApiOperation("客户模块-客户-UNI-删除分公司/分店")
    @GetMapping(value = "uDeleteSubDetail")
    public JsonData uDeleteSubDetail(
            @ApiParam(value = "客户编号") @RequestParam(value = "customer_boss_no", defaultValue = "") String customerBossNo,
            @ApiParam(value = "分店/分公司编号") @RequestParam(value = "customer_no", defaultValue = "") String customerNo) {
        return customerService.uDeleteSubDetail(customerBossNo, customerNo);
    }

    @ApiOperation("客户模块-客户-UNI-修改报备")
    @PostMapping(value = "uUpdateDetail", produces = "application/json;charset=UTF-8")
    public JsonData uUpdateDetail(
            @ApiParam(value = "uUpdateDetail") @RequestBody CustomerDTO customerDTO) {
        return customerService.uUpdateDetail(customerDTO);
    }


    @ApiOperation("客户模块-客户-UNI-报备详情")
    @GetMapping("uInfo")
    public JsonData uInfo(
            @ApiParam(value = "客户编号") @RequestParam(value = "customerNo", defaultValue = "") String customerNo) {
        return customerService.uInfo(customerNo);
    }

    @ApiOperation("客户模块-客户-UNI-列表详情")
    @GetMapping("uInfo2")
    public JsonData uInfo2(
            @ApiParam(value = "客户编号") @RequestParam(value = "customerNo", defaultValue = "") String customerNo) {
        return customerService.uInfo2(customerNo);
    }

    @ApiOperation("客户模块-客户-UNI-查看名录客户类型各项条目")
    @GetMapping("uCountCustomerType")
    public JsonData uCountCustomerType() {
        Map<String, Object> map = customerService.uCountCustomerType();
        return JsonData.buildSuccess(map);
    }

    @ApiOperation("客户模块-客户-UNI-查看拥有的分店/分公司,两个参数只能同时存在一个")
    @GetMapping("uSubSearch")
    public JsonData uSubSearch(
            @ApiParam(value = "所属公司或者门店编号") @RequestParam(value = "customerBossNo", defaultValue = "") String customerBossNo,
            @ApiParam(value = "客人所属公司或者门店编号") @RequestParam(value = "customerNo", defaultValue = "") String customerNo) {
        List<CustomerDTO> customerDTOList = customerService.uQuerySubCustomer(customerBossNo, customerNo);
        return JsonData.buildSuccess(customerDTOList);
    }


    @ApiOperation("客户模块-客户-UNI-客户名录列表（条件+分页）")
    @GetMapping("uSearch")
    public JsonData uSearch(
            @ApiParam(value = "当前页数") @RequestParam(value = "page", defaultValue = "1") int page,
            @ApiParam(value = "当页数目") @RequestParam(value = "size", defaultValue = "10") int size,
            @ApiParam(value = "关键字") @RequestParam(value = "keyWord", defaultValue = "") String keyWord,
            @ApiParam(value = "客户类型") @RequestParam(value = "customerType", defaultValue = "-1") int customerType,
            @ApiParam(value = "客户状态") @RequestParam(value = "customerStatus", defaultValue = "-1") int customerStatus,
            @ApiParam(value = "排序: 效益金额1，创建时间2，同行类别3") @RequestParam(value = "softOf", defaultValue = "-1") int softOf,
            @ApiParam(value = "是否为倒序，倒序1，正序不填或者非1") @RequestParam(value = "orderBy", defaultValue = "-1") int orderBy,
            @ApiParam(value = "地区") @RequestParam(value = "address", defaultValue = "") String address,
            @ApiParam(value = "客户类别: A类1，B类2，C类3 ... ") @RequestParam(value = "customerLevel", defaultValue = "-1") int customerLevel,
            @ApiParam(value = "业务进度") @RequestParam(value = "businessStatus", defaultValue = "-1") int businessStatus,
            @ApiParam(value = "距离上次来往/联系：1周内是7，2周内是14，按天数以此类推") @RequestParam(value = "lastBusinessDay", defaultValue = "-1") int lastBusinessDay) {
        Map<String, Object> customerDTOList = customerService.uQueryCustomer(page, size, keyWord, customerType, customerStatus, softOf, orderBy, address, customerLevel, businessStatus, lastBusinessDay);
        return JsonData.buildSuccess(customerDTOList);
    }

    @ApiOperation("客户模块-客户-UNI-客户名录列表（条件+不分页）")
    @GetMapping("uSearch2")
    public JsonData uSearch2(
            @ApiParam(value = "关键字") @RequestParam(value = "keyWord", defaultValue = "") String keyWord,
            @ApiParam(value = "客户类型") @RequestParam(value = "customerType", defaultValue = "-1") int customerType,
            @ApiParam(value = "客户状态") @RequestParam(value = "customerStatus", defaultValue = "-1") int customerStatus,
            @ApiParam(value = "排序: 效益金额1，创建时间2，同行类别3") @RequestParam(value = "softOf", defaultValue = "-1") int softOf,
            @ApiParam(value = "是否为倒序，倒序1，正序不填或者非1") @RequestParam(value = "orderBy", defaultValue = "-1") int orderBy,
            @ApiParam(value = "地区") @RequestParam(value = "address", defaultValue = "") String address,
            @ApiParam(value = "客户类别: A类1，B类2，C类3 ... ") @RequestParam(value = "customerLevel", defaultValue = "-1") int customerLevel,
            @ApiParam(value = "业务进度") @RequestParam(value = "businessStatus", defaultValue = "-1") int businessStatus,
            @ApiParam(value = "距离上次来往/联系：1周内是7，2周内是14，按天数以此类推") @RequestParam(value = "lastBusinessDay", defaultValue = "-1") int lastBusinessDay) {
        List<CustomerDTO> customerDTOList = customerService.uQueryCustomer2(keyWord, customerType, customerStatus, softOf, orderBy, address, customerLevel, businessStatus, lastBusinessDay);
        return JsonData.buildSuccess(customerDTOList);
    }

//    @ApiOperation("客户模块-客户-UNI-收藏")
//    @GetMapping("uCollect")
//    public JsonData uCollect(
//            @ApiParam(value = "客户编号") @RequestParam(value = "customerNo", defaultValue = "") String customerNo,
//            @ApiParam(value = "是否收藏，已收藏1，未收藏2") @RequestParam(value = "isCollect", defaultValue = "") int isCollect) {
//        return customerService.uCollect(customerNo, isCollect);
//    }

//    @ApiOperation("客户模块-客户-UNI-置顶")
//    @GetMapping("uPinTop")
//    public JsonData uPinTop(
//            @ApiParam(value = "客户编号") @RequestParam(value = "customerNo", defaultValue = "") String customerNo,
//            @ApiParam(value = "是否置顶，已置顶1，未置顶2") @RequestParam(value = "isPinTop", defaultValue = "") int isPinTop) {
//        return customerService.uPinTop(customerNo, isPinTop);
//    }

    @ApiOperation("客户模块-客户-UNI-客户移交")
    @GetMapping("uChangeBusinessId")
    public JsonData uChangeBusinessId(
            @ApiParam(value = "客户编号") @RequestParam(value = "customerNo", defaultValue = "") String customerNo,
            @ApiParam(value = "业务负责人id") @RequestParam(value = "businessManagerId", defaultValue = "") int businessManagerId,
            @ApiParam(value = "业务负责人") @RequestParam(value = "businessManager", defaultValue = "") String businessManager,
            @ApiParam(value = "业务部门id") @RequestParam(value = "businessDepartmentId", defaultValue = "") int businessDepartmentId,
            @ApiParam(value = "业务部门") @RequestParam(value = "businessDepartment", defaultValue = "") String businessDepartment,
            @ApiParam(value = "业务负责人电话") @RequestParam(value = "businessPhone", defaultValue = "") String businessPhone) {
        return customerService.uChangeBusinessId(customerNo, businessManagerId, businessManager, businessDepartmentId, businessDepartment, businessPhone);
    }


}

